package scenic.study.basic_knowledge;

import org.apache.log4j.Logger;
import org.junit.Test;
import scenic.MyLogger;

import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;



/**
 * Created by scenic on 2017/1/14.
 */

public class StreamTokenizerTest {

    static Logger logger = MyLogger.get(StreamTokenizerTest.class);

    public static void main(String args[]) throws IOException {
//        String str = "( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) a a / . ";
        String str =  "(1)+((2+3)*(4*5)))-9";
        StreamTokenizer streamTokenizer = new StreamTokenizer(new StringReader(str));
        if(true){
//            streamTokenizer.ordinaryChar(' ');
//            streamTokenizer.ordinaryChar('/');
//            streamTokenizer.ordinaryChar('.');
//            streamTokenizer.ordinaryChar('a');
//            streamTokenizer.wordChars('_', '_');
//            streamTokenizer.eolIsSignificant(false);
                streamTokenizer.ordinaryChar('a');
                streamTokenizer.commentChar('-');
        }


        while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
            int ttype = streamTokenizer.ttype;
            String sval = streamTokenizer.sval;
            double nval = streamTokenizer.nval;
            String lineNumber = "   line , " + streamTokenizer.lineno() ;

            logger.debug(streamTokenizer.toString());

            switch (ttype) {
                case StreamTokenizer.TT_EOF:
                    logger.debug("eof" + lineNumber);
                    break;
                case StreamTokenizer.TT_EOL:
                    logger.debug("eol" + lineNumber);
                    break;
                case StreamTokenizer.TT_WORD:
                    logger.debug("word\t\t" + sval + lineNumber);
                    break;
                case StreamTokenizer.TT_NUMBER:
                    logger.debug("number\t\t" + nval + lineNumber);
                    break;
//                case streamTokenizer.TT_NOTHING:
//                    ret = "NOTHING";
//                    break;
                default: {
                /*
                 * ttype is the first character of either a quoted string or
                 * is an ordinary character. ttype can definitely not be less
                 * than 0, since those are reserved values used in the previous
                 * case statements
                 */
//                    if (ttype < 256 &&
//                            ((ctype[ttype] & CT_QUOTE) != 0)) {
//                        ret = sval;
//                        break;
//                    }


//                    char s[] = new char[3];
//                    s[0] = s[2] = '\'';
//                    s[1] = (char) ttype;
//                    String ret = new String(s);
                    String ret = new String(new char[] {(char) ttype });

                    logger.debug("default\t\t" + ret + lineNumber);
                    break;
                }
            }


        }

    }

    @Test
    public void showASIK() {
        for (int i = 0; i < 255; i++) {
            logger.debug(i + "\t\t" + (char)i);
        }
    }

}
